#include "PauseLayer.h"
#include "Resources.h"
#include "VisibleRect.h"
#include "Global.h"
#include "GameMacros.h"
#include "GameMenu.h"

bool PauseLayer::init()
{
	bool bRet = false;
	do 
	{
		CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize();
		CC_BREAK_IF(!CCLayerColor::initWithColor(ccc4(108, 109, 107, 255), screenSize.width, screenSize.height));
		//this->setContentSize(CCEGLView::sharedOpenGLView()->getFrameSize());
		//CCRect layerRect = CCRectMake(0, 412, 618, 412);
		//CCTexture2D *pTexturePause = CCTextureCache::sharedTextureCache()->textureForKey(s_screens1);
		//CCSprite *pPause = CCSprite::createWithTexture(pTexturePause, layerRect);
		//pPause->setPosition(VisibleRect::center());	
		//addChild(pPause);

		CCSprite *pPause = CCSprite::create(s_screens1, CCRectMake(250, 490, 120, 60));
		pPause->setScale(1.5f);
		pPause->setPosition(ccpSub(VisibleRect::top(), ccp(0, 100)));

		CCTextureCache *pCache = CCTextureCache::sharedTextureCache();
		CCRect rect1 = CCRectMake(170, 580,	260, 70);
		CCTexture2D *pT = pCache->textureForKey(s_screens1);
		CCSprite *pN1 = CCSprite::createWithTexture(pT, rect1);
		CCSprite *pS1 = CCSprite::createWithTexture(pT, rect1);
		CCSprite *pD1 = CCSprite::createWithTexture(pT, rect1);
		CCRect rect2 = CCRectMake(170, 650,	260, 70);
		CCSprite *pN2 = CCSprite::createWithTexture(pT, rect2);
		CCSprite *pS2 = CCSprite::createWithTexture(pT, rect2);
		CCSprite *pD2 = CCSprite::createWithTexture(pT, rect2);
		CCRect rect3 = CCRectMake(170, 730,	260, 70);
		CCSprite *pN3 = CCSprite::createWithTexture(pT, rect3);
		CCSprite *pS3 = CCSprite::createWithTexture(pT, rect3);
		CCSprite *pD3 = CCSprite::createWithTexture(pT, rect3);

		CCMenuItemSprite *pItem1 = CCMenuItemSprite::create(pN1, pS1, pD1, this, menu_selector(PauseLayer::backToGame));
		CCMenuItemSprite *pItem2 = CCMenuItemSprite::create(pN2, pS2, pD2, this, menu_selector(PauseLayer::backToMenu));
		CCMenuItemSprite *pItem3 = CCMenuItemSprite::create(pN3, pS3, pD3, this, menu_selector(PauseLayer::quit));
		pItem1->setScale(1.5f);
		pItem2->setScale(1.5f);
		pItem3->setScale(1.5f);
		pItem1->setPosition(VisibleRect::center());
		CCMenu *pMenu = CCMenu::create(pItem1, pItem2, pItem3, NULL);
		pMenu->setPosition(VisibleRect::bottom().x, VisibleRect::bottom().y + 250);
		pMenu->alignItemsVerticallyWithPadding(50);
		addChild(pMenu);
		addChild(pPause);
		bRet = true;
	} while (false);
	return bRet;
}

void PauseLayer::keyBackClicked()
{
	CCDirector::sharedDirector()->resume();
	this->removeFromParent();
}

void PauseLayer::backToMenu(CCObject *pSender)
{
	CCDirector::sharedDirector()->resume();
	REPLACE_SCENE(CCTransitionFade, 0.5f, GameMenu);
}

void PauseLayer::backToGame(CCObject *pSender)
{
	CCDirector::sharedDirector()->resume();
	this->removeFromParent();
}

void PauseLayer::quit(CCObject *pSender)
{
	CCDirector::sharedDirector()->end();
}